package com.beck.security.service;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Collection;

//@Component("myUserDetailsService")
public class MyUserDetailsService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 如果username不存在，则抛出UsernameNotFoundException异常，
        // 若username存在，则获得该用户的密码及角色返回，由框架判断密码是否正确
        //
        System.out.println("-----------------------------------------");
        String password = new BCryptPasswordEncoder().encode("123456");
        Collection<GrantedAuthority> auths =
                AuthorityUtils.commaSeparatedStringToAuthorityList("vip1,vip2");
        if(username.equals("tom")) throw new UsernameNotFoundException("tom不存在！！！");
        // 用户名，密码，角色
        return new User(username,password,auths);
    }
}
